
function TreeNode(val, left, right) {
    this.val = (val===undefined ? 0 : val)
    this.left = (left===undefined ? null : left)
    this.right = (right===undefined ? null : right)
}

/**
 * @param {number[]} inorder
 * @param {number[]} postorder
 * @return {TreeNode}
 */
var buildTree = function(inorder, postorder) {
  if(postorder.length === 0)
    return null;
  // 拿到最后一个
  let value = postorder[postorder.length - 1];
  //
  let root = new TreeNode(value), index = inorder.indexOf(value);
  if(postorder.length === 1)
    return root;
  let length = inorder.slice(0, index).length;
  root.left = buildTree(inorder.slice(0, index), postorder.slice(0, length));
  root.right = buildTree(inorder.slice(index + 1, inorder.length), postorder.slice(length, postorder.length - 1));
  return root;
};
let inorder = [9,3,15,20,7], postorder = [9,15,7,20,3];
console.log(buildTree(inorder, postorder));
inorder = [-1], postorder = [-1];
console.log(buildTree(inorder, postorder));
// let index = 1;
// console.log(inorder.slice(0, index), inorder.slice(index + 1, inorder.length));
// console.log(postorder.slice(0, index + 1), postorder.slice(index + 1, postorder.length - 1));

